package cn.est.serviceImpl;

import cn.est.constants.Constants;
import cn.est.dto.Page;
import cn.est.mapper.PayOrderMapper;
import cn.est.pojo.MaintainOrder;
import cn.est.pojo.PayOrder;
import cn.est.service.PayOrderService;
import cn.est.utils.DateUtils;
import cn.est.utils.MathUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PayOrderServiceImpl implements PayOrderService {

    @Autowired
    private PayOrderMapper payOrderMapper;

    public PayOrder getPayOrderById(Long id) {
        return payOrderMapper.getPayOrderById(id);
    }

    public List<PayOrder> getPayOrderListByMap(Map<String, Object> param) {
        return payOrderMapper.getPayOrderListByMap(param);
    }

    public Integer getPayOrderCountByMap(Map<String, Object> param) {
        return payOrderMapper.getPayOrderCountByMap(param);
    }

    public Integer qdtxAddPayOrder(PayOrder payOrder) {
        payOrder.setCreatdTime(new Date());
        return payOrderMapper.insertPayOrder(payOrder);
    }

    public Integer qdtxModifyPayOrder(PayOrder payOrder) {
        payOrder.setUpdatedTime(new Date());
        return payOrderMapper.updatePayOrder(payOrder);
    }

    public Integer qdtxDeletePayOrderById(Long id) {
        return payOrderMapper.deletePayOrderById(id);
    }

    public Integer qdtxBatchDeletePayOrder(String ids) {
        Map<String, List<String>> param = new HashMap<String, List<String>>();
        String[] paramArrays = ids.split(",");
        List<String> idList = new ArrayList<String>();
        for (String temp : paramArrays) {
            idList.add(temp);
        }
        param.put("ids", idList);
        return payOrderMapper.batchDeletePayOrder(param);
    }

    public Page<PayOrder> queryPayOrderPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) {
        Integer total = payOrderMapper.getPayOrderCountByMap(param);
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<PayOrder> payOrderList = payOrderMapper.getPayOrderListByMap(param);
        page.setList(payOrderList);
        return page;
    }

    @Override
    public PayOrder save(MaintainOrder maintainOrder, Integer type) {
        //1.orderNo
          String orderNo= maintainOrder.getOrderNo();
          Integer payType;
         if(maintainOrder.getStatus().equals(Constants.Order.MaintainStatus.APPOINT)){
             payType=Constants.Order.MaintainStatus.PAY_DEPOSIT;
         }
         //2.创建支付订单编号
         String payNo=createPayNo(maintainOrder.getOrderNo(),type);
            PayOrder payOrder =new PayOrder();
         payOrder.setOrderNo(maintainOrder.getOrderNo());
             payOrder.setUserId(maintainOrder.getUserId());
         payOrder.setPayNo(payNo);
         payOrder.setPrice(maintainOrder.getSubscription());
         payOrder.setPayType(type);
         payOrder.setStatus(Constants.Order.PayStaus.UN_PAY);
         payOrder.setPayChannel(Constants.Order.Channel.ALIPAY);
         payOrder.setCreatedUserId(maintainOrder.getUserId());
         payOrder.setIsDelete(0);
         payOrder.setCreatdTime(new Date());
        //3.插入数据库
        payOrderMapper.insertPayOrder(payOrder);

        return payOrder;
    }

    @Override
    public void finishPay(PayOrder payOrder, String aliTradeNo) {
        payOrder.setAliTradeNo(aliTradeNo);
        payOrder.setStatus(Constants.Order.PayStaus.SUCCESS);
        payOrder.setUpdatedUserId(payOrder.getUserId());
        payOrder.setCreatdTime(DateUtils.getCurrentDate(DateUtils.YYYY_MM_DD_HH_MM_SS));
        payOrderMapper.updatePayOrder(payOrder);

    }


    /**
     * 创建一个支付订单编号、
     *
     * 订单号+随机数+交易类型
     * @param orderNo
     * @return
     */
    private String createPayNo(String orderNo,Integer type){
        StringBuffer stringBuffer =new StringBuffer(orderNo);
        stringBuffer.append(MathUtils.random());
        stringBuffer.append(type);
        String payNo=stringBuffer.toString();
        Map map =new HashMap();
        map.put("payNo",payNo);
        List<PayOrder> payOrderList=   payOrderMapper.getPayOrderListByMap(map);
        if(payOrderList.size()>0){
            return createPayNo(orderNo,type);
        }
        return payNo;
    }

}
